<?php
namespace app\admin\controller;

use app\admin\model\{Setting,AdminUser as AdminUserModel};
use think\facade\{Env,Cache};

/**
 * 后台开放接口
 * Class Publics
 * @package app\admin\Publics
 */
class Publics extends Jwt
{
    public function login()
    {
        // 接收参数
        $param = $this->request->param();
        // 实例化数据库
        $AdminUserModel = new AdminUserModel;
        // 登录
        if ($user = $AdminUserModel->login($param) ){
            $token = $this->getToken(['iss'=>'admin','userinfo'=>$user,'iat'=>time(),'exp'=>time()+70000,'nbf'=>time(),'sub'=> Env::get('host.local_host'),'jti'=>md5(uniqid('JWT').time())]);
            // 给缓存注入新的过期时间
            Cache::tag('cache')->set('token_'.$user['admin_user_id'],$token,1800);
            // 返回签名
            return $this->renderSuccess(['token'=>$token],'登录成功');
        }
        // 返回错误信息
        return $this->renderError($AdminUserModel->getError() ?? '登录失败');
    }

    public function getConfiguration()
    {
        $setting = Setting::getItem('setting');
        $setting['title'] = htmlspecialchars_decode($setting['name']);
        return $this->renderSuccess($setting,'获取基础配置成功!');
    }

    // 返回验证码key给前端
    public function getVerifyKey()
    {     
        // 获取图片验证码对应信息
        return $this->renderSuccess(['key'=>getVerifyKey()]);
    }
    public function verify()
    {    // 获取图片验证码对应信息
        return setVerify();
    }
}